<?php

namespace app\controllers;

use Yii;
use yii\rbac\Item;
use yii\rbac\Role;
use yii\rbac\Permission;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
use helper\SessionHelper;

class AdminuserController extends SecBaseController
{
    public function actionList(){
        $request = Yii::$app->request;
        $page_code = $request->get('page_code');
        $sort_code = $request->get('sort_code');
        $sortorder = $request->get('sortorder');
        $user_name = $request->get('user_name');
        $vender_id = $request->get('vender_id');
        $this->remote_params['page_code'] = $page_code;
        $this->remote_params['sort_code'] = $sort_code;
        $this->remote_params['sortorder'] = $sortorder;
        $this->remote_params['user_name'] = $user_name;
        $this->remote_params['vender_id'] = $vender_id;

        try{
            $ret = $this->proxy->execute('admin', 'list', $this->remote_params);
            //$this->return_page_values = array_merge($this->return_page_values, $ret);
        }catch(\Exception $e){
            $this->return_page_values['errno'] = $e->getCode();
            $this->return_page_values['errmsg'] = $e->getMessage();
            return $this->myRenderFile('@tpl/page/error.tpl', $this->return_page_values);
        }

        $authManager = Yii::$app->authManager;

        //增加数据中的角色信息
        foreach($ret["data_list"] as $key => $admin) {
            $role_items = $authManager->getRolesByUser($admin["user_id"]);
            $roles = array();
            foreach ($role_items as $item) {
                $roles[] = $item->name;
            }
            $ret["data_list"][$key]["roles"] = $roles;
        }
        //增加数据中的角色标题信息
        $roles_column = array("key"=>"roles", "name"=>"所属角色", "type"=>"array", "customMap" => array("url" => "/basedata/rbacitems?type=".Item::TYPE_ROLE, "option_value" => "id", "option_text" => "name"), "editable" => 1, "intable" => 0, "show_key" => null);
        $ret["column_title"][] = $roles_column;

        $this->return_page_values = array_merge($this->return_page_values, $ret);

        return $this->myRenderFile('@tpl/page/adminuser/list.tpl', $this->return_page_values);
    }

    public function actionCreate(){
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;

        $request = Yii::$app->request;
        $thisRequestParams = $request->getQueryParams();
        $thisBodyParams = $request->getBodyParams();

        $req_params =  array_merge($this->remote_params, $thisRequestParams, $thisBodyParams);
        $new_params =  array();
        foreach ($req_params as $key => $value) {
            if(is_array($value)){
                foreach ($value as $k => $v) {
                    if(isset($new_params[$key])){
                        $new_params[$key] = $new_params[$key].','.$v;
                    }else{
                        $new_params[$key] = $v;
                    }
   
                }
            }else{
                $new_params[$key] = $value;
            }
        }

        try{
            $ret = $this->proxy->execute("admin", "create",$new_params );
        }catch(\Exception $e){
             return ['errno' => $e->getCode(), 'errmsg' => $e->getMessage()];
        }

        $authManager = Yii::$app->authManager;
        $roles = $request->post("roles");
        if (!empty($roles)) {
            foreach($roles as $role) {
                $role_item = $authManager->getRole($role);
                try {
                    $authManager->assign($role_item, $ret["admin_user"]["user_id"]);
                } catch(\Exception $e){
                    $this->return_page_values['errno'] = -1;
                    $this->return_page_values['errmsg'] = 'assign role failed: '.$e->getMessage();
                    return $this->return_page_values;
                }
            } 
        }

        $ret = array("errno" => 0);
        return $ret;
    }

    public function actionUpdate(){
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;

        $request = Yii::$app->request;
        $thisRequestParams = $request->getQueryParams();
        $thisBodyParams = $request->getBodyParams();

        $req_params =  array_merge($this->remote_params, $thisRequestParams, $thisBodyParams);
        $new_params =  array();
        foreach ($req_params as $key => $value) {
            if(is_array($value)){
                foreach ($value as $k => $v) {
                    if(isset($new_params[$key])){
                        $new_params[$key] = $new_params[$key].','.$v;
                    }else{
                        $new_params[$key] = $v;
                    }
   
                }
            }else{
                $new_params[$key] = $value;
            }
        }

        try{
            $ret = $this->proxy->execute("admin", "update",$new_params );
        }catch(\Exception $e){
             return ['errno' => $e->getCode(), 'errmsg' => $e->getMessage()];
        }

        $authManager = Yii::$app->authManager;
        $roles = $request->post("roles");
        if (!empty($roles)) {
            foreach($roles as $role) {
                $role_item = $authManager->getRole($role);
                try {
                    $assignment = $authManager->getAssignment($role_item->name, $ret["admin_user"]["user_id"]);
                    if (empty($assignment)) {
                        $authManager->assign($role_item, $ret["admin_user"]["user_id"]);
                    }
                } catch(\Exception $e){
                    $this->return_page_values['errno'] = -1;
                    $this->return_page_values['errmsg'] = 'assign role failed: '.$e->getMessage();
                    return $this->return_page_values;
                }
            } 
        }

        $ret = array("errno" => 0);
        return $ret;
    }
 
 
    public function actionDelete(){
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;

        $request = Yii::$app->request;
        $thisRequestParams = $request->getQueryParams();
        $thisBodyParams = $request->getBodyParams();

        $req_params =  array_merge($this->remote_params, $thisRequestParams, $thisBodyParams);
        $new_params =  array();
        foreach ($req_params as $key => $value) {
            if(is_array($value)){
                foreach ($value as $k => $v) {
                    if(isset($new_params[$key])){
                        $new_params[$key] = $new_params[$key].','.$v;
                    }else{
                        $new_params[$key] = $v;
                    }

                }
            }else{
                $new_params[$key] = $value;
            }
        }


        try{
            $ret = $this->proxy->execute("admin", "delete",$new_params );
        }catch(\Exception $e){
             return ['errno' => $e->getCode(), 'errmsg' => $e->getMessage()];
        }

        return $ret;
    }   
 
}
